package com.xingdata.jjxc.m.login.avt;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.ab.activity.AbActivity;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.m.avt.Mainavt;
import com.xingdata.jjxc.m.avt.TestActivity;
import com.xingdata.jjxc.m.login.adp.ViewPagerAdapter;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.SharedConfig;
import com.xingdata.jjxc.views.Widget;

public class Welcom extends Activity implements OnClickListener,
		OnPageChangeListener {
	private ViewPager viewPager;
	private PagerAdapter pagerAdapter;
	private Button startButton;
	// private LinearLayout indicatorLayout;
	private ArrayList<View> views;
	private int[] images;
	private ImageView[] indicators = null;
	private ArrayList<ImageView> imageViews = new ArrayList<ImageView>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcom);

//		ExitApplication.getInstance().addActivity(this);

		images = new int[] { R.drawable.welcome_01, R.drawable.welcome_02,
				R.drawable.welcome_03 };

		initView();
	}

	/**
	 * 初始化视图
	 */
	private void initView() {
		// 实例化视图控件
		viewPager = (ViewPager) findViewById(R.id.viewpage);
		startButton = (Button) findViewById(R.id.start_Button);
		startButton.setOnClickListener(this);
		// indicatorLayout = (LinearLayout) findViewById(R.id.indicator);
		views = new ArrayList<View>();
		indicators = new ImageView[images.length];
		for (int i = 0; i < images.length; i++) {
			// 循环加入图片
			ImageView imageView = new ImageView(this);
			imageView.setBackgroundResource(images[i]);
			views.add(imageView);
			imageViews.add(imageView);
			// 循环加入指示器
			indicators[i] = new ImageView(this);
			indicators[i].setBackgroundResource(R.drawable.indicators_default);
			if (i == 0) {
				indicators[i].setBackgroundResource(R.drawable.indicators_now);
			}
			// indicatorLayout.addView(indicators[i]);
		}
		pagerAdapter = new ViewPagerAdapter(views);
		viewPager.setAdapter(pagerAdapter); // 设置适配器
		viewPager.setOnPageChangeListener(this);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// 显示最后一个图片时显示按钮
		if (arg0 == indicators.length - 1) {
			startButton.setVisibility(View.VISIBLE);
		} else {
			startButton.setVisibility(View.INVISIBLE);
		}
		// 更改指示器图片
		for (int i = 0; i < indicators.length; i++) {
			indicators[arg0].setBackgroundResource(R.drawable.indicators_now);
			if (arg0 != i) {
				indicators[i]
						.setBackgroundResource(R.drawable.indicators_default);
			}
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.start_Button) {
			SharedPreferences shared = new SharedConfig(this).GetConfig();
			Editor editor = shared.edit();
			editor.putBoolean("First", false);
			editor.commit();
			Intent intent = new Intent(Welcom.this, Mainavt.class);
			startActivity(intent);

			Widget.startActivityAnim(Welcom.this, R.anim.fade_in,
					R.anim.fade_out);
			finish();

		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// if (viewPager != null) {
		// viewPager.removeAllViews();
		// viewPager.destroyDrawingCache();
		// }
		/**
		 * 回收所有图片
		 */
		for (int i = 0; i < imageViews.size(); i++) {
			Drawable drawable = imageViews.get(i).getDrawable();
			if (drawable != null) {
				drawable.setCallback(null);
			}
			imageViews.get(i).setImageBitmap(null);
			imageViews.get(i).setBackgroundDrawable(null);

		}
	}
}
